/**
 * @author xuzj
 */
CQCIS.tree.ColumnTreeSelectionModel = function(){
    this.selNodes = [];
    this.selMap = {};
    this.addEvents({
        "selectionchange": true
    });
};
Ext.extend(CQCIS.tree.ColumnTreeSelectionModel, Ext.tree.DefaultSelectionModel, {
    select: function(node, e){
        if (this.isSelected(node)) {
            this.lastSelNode = node;
            node.unselect();
            return node;
        }
        this.selNodes.push(node);
        this.selMap[node.id] = node;
        this.lastSelNode = node;
        if (!node.isRoot) {
            this.toggleCheckPar(node, true);
        }
        node.ui.onSelectedChange(true);
        this.fireEvent("selectionchange", this, this.selNodes);
        return node;
    },
    unselect: function(node){
        if (this.selMap[node.id]) {
            node.ui.onSelectedChange(false);
            var sn = this.selNodes;
            var index = -1;
            if (sn.indexOf) {
                index = sn.indexOf(node);
            } else {
                for (var i = 0, len = sn.length; i < len; i++) {
                    if (sn[i] == node) {
                        index = i;
                        break;
                    }
                }
            }
            if (index != -1) {
                this.selNodes.splice(index, 1);
            }
            delete this.selMap[node.id];
            if (!node.isRoot) {
                this.toggleCheckPar(node, false);
            }
            this.unSelectChildren(node);
            this.fireEvent("selectionchange", this, this.selNodes);
        }
    },
    unSelectChildren: function(node){
        var children = node.childNodes;
        if (!children || children == 0) {
            return false;
        } else {
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (child.isSelected()) {
                    child.unselect();
                }
            }
        }
    },
    toggleCheckPar: function(node, state){
        var parNodeUI = node.parentNode.getUI();
        if (parNodeUI.checkbox) {
            if (state) {
                if (!parNodeUI.node.isSelected()) {
                    parNodeUI.node.select();
                }
                if (!parNodeUI.node.isRoot) {
                    this.toggleCheckPar(parNodeUI.node, state);
                }
            } else {
                var bros = node.parentNode.childNodes;
                var flag = false;
                for (var i = 0; i < bros.length; i++) {
                    if (bros[i]) {
                        var broUI = bros[i].getUI();
                        if (broUI.checkbox && broUI.checkbox.checked) {
                            flag = true;
                            break;
                        }
                    } else {
                        return false;
                    }
                }
                if (flag) {
                    return false;
                } else {
                    parNodeUI.node.unselect();
                    if (!parNodeUI.node.isRoot) {
                        this.toggleCheckPar(parNodeUI.node, state);
                    }
                }
            }
        } else {
            return false;
        }
    },
    getSelectedNodes: function(){
        return this.selNodes;
    },
    isSelected: function(node){
        return this.selMap[node.id] ? true : false;
    },
    getSelectedRows: function(){
        var buf = new Array();
        var key;
        var columns = this.tree.columns;
        for (var i = 0; i < columns.length; i++) {
            if (columns[i].id) {
                key = columns[i].id;
                break;
            }
        }
        for (var i = 0; i < this.selNodes.length; i++) {
            eval("buf.push({" + key + " : this.selNodes[i].getUI().checkbox.value});");
        }
        return buf;
    }
});
